<?php
define('KC_FILE_COMMAND', '/usr/bin/file -ib "%s"');
require_once ("class-IXR.php");
require_once ("wp-api-post.php");

class Utils
{

    public static function getRssTail ()
    {
        $RssTail = "</channel>" . PHP_EOL . "</rss>";
        return $RssTail;
    }

    public static function getRssHeader ()
    {
        $RssHeader = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><rss version=\"2.0\"" .
                 PHP_EOL .
                 "xmlns:content=\"http://purl.org/rss/1.0/modules/content/\"" .
                 PHP_EOL . "xmlns:wfw=\"http://wellformedweb.org/CommentAPI/\"" .
                 PHP_EOL . "xmlns:dc=\"http://purl.org/dc/elements/1.1/\"" .
                 PHP_EOL . "xmlns:atom=\"http://www.w3.org/2005/Atom\"" . PHP_EOL .
                 "xmlns:sy=\"http://purl.org/rss/1.0/modules/syndication/\"" .
                 PHP_EOL .
                 "xmlns:slash=\"http://purl.org/rss/1.0/modules/slash/\">" .
                 PHP_EOL . "<channel>" . PHP_EOL . "<title>" . PHP_EOL .
                 "<![CDATA[ ��Ц����Ӫ ]]>" . PHP_EOL . "</title>" . PHP_EOL .
                 "<description>" . PHP_EOL . "<![CDATA[" . PHP_EOL .
                 "��Ц����Ӫ - �ں�ͼ,а������,����ͼ,Ц����ȫ�ַ���,��Ц����ѡ,,��Ц��ƵͼƬ,Ц����ȫ ��Ц,ÿ����´�������˼�����ݣ�" .
                 PHP_EOL . "]]>" . PHP_EOL . "</description>" . PHP_EOL .
                 "<link>http://www.idreems.com/</link>" . PHP_EOL .
                 "<language>zh-cn</language>" . PHP_EOL .
                 "<generator>www.idreems.com</generator>" . PHP_EOL;
        return $RssHeader;
    }

    /**
     * return data path for file
     *
     * @return string
     */
    public static function getDataPath ()
    {
        return "data\\";
    }

    public static function getTime ($date_format, $time)
    {
        date_default_timezone_set('PRC');
        return date($date_format, $time);
    }

    public static function getToday ($date_format)
    {
        date_default_timezone_set('PRC');
        return date($date_format, time());
    }

    public static function getYesterday ($date_format)
    {
        date_default_timezone_set('PRC');
        return date($date_format, strtotime("-1 day"));
    }

    public static function getTommorrow ($date_format)
    {
        date_default_timezone_set('PRC');
        return date($date_format, strtotime("+1 day"));
    }

    public static function getRealIpAddr ()
    {
        if (! empty($_SERVER['HTTP_CLIENT_IP']))         // check ip from share
                                                 // internet
        {
            $ip = $_SERVER['HTTP_CLIENT_IP'];
        } elseif (! empty($_SERVER['HTTP_X_FORWARDED_FOR']))         // to check ip
                                                             // is
                                                             // pass from
                                                             // proxy
        {
            $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
        } else {
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        return $ip;
    }

    public static function mail ($to, $subject = "", $body = "")
    {
        if (false == mail($to, $subject, $body)) {
            echo "fail to send mail";
        }
    }

    public static function array_to_json ($data)
    {
        if (is_array($data) || is_object($data)) {
            $islist = is_array($data) && (empty($data) ||
                     array_keys($data) === range(0, count($data) - 1));
            if ($islist)
                $json = '[' . implode(',', array_map('php_json_encode', $data)) .
                         ']';
            else {
                $items = Array();
                foreach ($data as $key => $value)
                    $items[] = Utils::array_to_json("$key") . ':' .
                             Utils::array_to_json($value);
                $json = '{' . implode(',', $items) . '}';
            }
        } elseif (is_string($data)) {
            $string = '"' . addcslashes($data, "\\\"\n\r\t/" . chr(8) . chr(12)) .
                     '"';
            
            $json = '';
            $len = strlen($string);
            for ($i = 0; $i < $len; $i ++) {
                $char = $string[$i];
                
                $c1 = ord($char);
                if ($c1 < 128) {
                    $json .= ($c1 > 31) ? $char : sprintf("\\u%04x", $c1);
                    continue;
                }
                $c2 = ord($string[++ $i]);
                if (($c1 & 32) === 0) {
                    $json .= sprintf("\\u%04x", ($c1 - 192) * 64 + $c2 - 128);
                    continue;
                }
                $c3 = ord($string[++ $i]);
                if (($c1 & 16) === 0) {
                    $json .= sprintf("\\u%04x", 
                            (($c1 - 224) << 12) + (($c2 - 128) << 6) +
                                     ($c3 - 128));
                    continue;
                }
                $c4 = ord($string[++ $i]);
                if (($c1 & 8) === 0) {
                    $u = (($c1 & 15) << 2) + (($c2 >> 4) & 3) - 1;
                    $w1 = (54 << 10) + ($u << 6) + (($c2 & 15) << 2) +
                             (($c3 >> 4) & 3);
                    $w2 = (55 << 10) + (($c3 & 15) << 6) + ($c4 - 128);
                    $json .= sprintf("\\u%04x\\u%04x", $w1, $w2);
                }
            }
        } else
            $json = strtolower(var_export($data, true));
        
        return $json;
    }

    public static function xor_this ($string, $key)
    {
        // Our plaintext/ciphertext
        $text = $string;
        
        // Our output text
        $outText = '';
        
        // Iterate through each character
        for ($i = 0; $i < strlen($text);) {
            for ($j = 0; $j < strlen($key); $j ++, $i ++) {
                $outText .= $text{$i} ^ $key{$j};
            }
        }
        return $outText;
    }

    public static function postBlog ($wpsite, $username, $password, $originTitle, 
            $originBody, $originalTag,$draft,$gbk)
    {
        $GLOBALS['xmlrpc_internalencoding'] = 'UTF-8';
        
        // $wpsite = 'http://www.idreems.com/wordpress';
        $wp = new wp_api_post($username, $password, $wpsite);
        $wp->set_min_max_day(3, 5);
        
        if ($gbk) {
            $title = iconv("GBK", "UTF-8", $originTitle);
            $body = iconv("GBK", "UTF-8", $originBody);
            $tag = iconv("GBK", "UTF-8", $originalTag);
        } else {
            $title = $originTitle;
            $body = $originBody;
            $tag = $originalTag;
        }
        $posts = array(
                'title' => $title,
                'body' => $body,
                // 'categories' => array(
                // $category
                // ),
                "Draft"=>$draft,
                'keywords' => array(
                        $tag
                )
        );
        
        if (false == $wp->wp_post($posts)) {
            exit(0);
        } else {
            exit(- 1);
        }
    }
}
?>